package homework.h03;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/10/6 19:15
 * @package homework.h03
 * @description 文件说明
 */

public class Vehicle {
    // 注册码
    private String registrationCode;

    // 制造商
    private String manufacturer;

    // 生产年份
    private Date date;

    // 价格
    private double price;

    public Vehicle(String registrationCode, String manufacturer, Date date) {
        this.registrationCode = registrationCode;
        this.manufacturer = manufacturer;
        this.date = date;
    }

    public String getRegistrationCode() {
        return registrationCode;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public Date getDate() {
        return date;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String compareCarAge(Date date) {
        Long starTime = this.date.getTime();
        Long endTime = date.getTime();
        Long num = endTime - starTime;//时间戳相差的毫秒数
        return String.format("车龄为：%.1f 年", (num / 24 / 60 / 60 / 1000 / 365.0));

    }

    @Override
    public String toString() {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = ft.format(this.date);
        return "注册码：" + registrationCode +
                ",汽车制造商：" + manufacturer +
                ",生产日期：" + formatDate +
                ",价格" + String.format("%.2f", price);
    }

    public static void main(String[] args) throws ParseException {

        // 创建一个 Vehicle 对象
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse("2009-06-01");
        Vehicle vehicle = new Vehicle("vic121123123", "沃尔沃", date);
        // 设置价格
        vehicle.setPrice(120000);
        System.out.println(vehicle);


        // 计算车龄
        Date today = new Date();
        System.out.println(vehicle.compareCarAge(today));


    }
}


class SecondHandVehicle extends Vehicle {
    // 二手车交易次数
    int numberOfOwner;

    public SecondHandVehicle(String registrationCode, String manufacturer, Date date, int numberOfOwner) {
        super(registrationCode, manufacturer, date);

        this.numberOfOwner = numberOfOwner;
    }

    public int getNumberOfOwner() {
        return numberOfOwner;
    }

    public void setNumberOfOwner(int numberOfOwner) {
        this.numberOfOwner = numberOfOwner;
    }

    @Override
    public String toString() {
        return super.toString() + ",交易次数：" + numberOfOwner;
    }
}
class TestCar {
    public static void main(String[] args) throws ParseException {
        SecondHandVehicle vehicle =
                new SecondHandVehicle("vic22012001",
                        "沃尔沃",
                        new SimpleDateFormat("yyyy-MM-dd").parse("2009-06-01"),
                        2);

        vehicle.setPrice(123123.32);

        System.out.println(vehicle);

        Date today = new Date();
        System.out.println(vehicle.compareCarAge(today));

        System.out.println("交易次数：" + vehicle.getNumberOfOwner());
    }
}



